package cn.lxg.utils;

import cn.hutool.core.collection.CollectionUtil;

import java.util.*;

public class LotteryUtils {


    /**
     * 获取不能出现的数字
     *
     * @param nums
     * @return
     */
    public static Set<Integer> getCannotAppearNum(List<Integer> nums) {
        // 使用 HashMap 来记录每个数字的出现频率
        Map<Integer, Integer> frequencyMap = new HashMap<>();

        for (int num : nums) {
            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
        }
        //获取最多次数最
        Integer max = CollectionUtil.max(frequencyMap.values());
        //距离最近的值
        int closestValue = RangeUtil.findClosestValue(frequencyMap.values().stream().toList(), max);
        //返回数组
        Set<Integer> result = new HashSet<>();
        for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            if (entry.getValue().equals(max) || entry.getValue() >= closestValue) {
                result.add(entry.getKey());
            }
        }
        return result;
    }


    /**
     * 根据历史最多次出现的 和 最接近的
     *
     * @return
     */
    public static Set<Integer> getCannotNumByLaterAndAllHistory(List<Integer> allHistoryNum, List<Integer> latelyDayNum) {
        // 使用 HashMap 来记录每个数字的出现频率
        Map<Integer, Integer> frequencyMap = new HashMap<>();

        for (int num : allHistoryNum) {
            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
        }
        //获取最多次数最
        Integer max = CollectionUtil.max(frequencyMap.values());
        //距离最近的值
        int closestValue = RangeUtil.findClosestValue(frequencyMap.values().stream().toList(), max);
        double asDouble =  frequencyMap.values().stream().mapToInt(s -> s).average().getAsDouble() + (frequencyMap.values().stream().mapToInt(s -> s).average().getAsDouble() + ((double) allHistoryNum.size() / frequencyMap.keySet().size())) / frequencyMap.keySet().size() + 1;
        //返回数组
        Set<Integer> result = new HashSet<>();
        for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
            Integer value = entry.getValue();
            Integer key = entry.getKey();
            if (value.equals(max) || value >= closestValue) {
                result.add(entry.getKey());
                continue;
            }

            if (value >= asDouble && latelyDayNum.contains(key)) {
                result.add(key);
            }

        }
        return result;
    }

}
